﻿;
; AHK Version: 	AHK_L Unicode 1.0.97.1
; Language:    	English
; Platform:    	>= Windows XP x86 (Developed on Win7 x64) 
; Author:      	B.Hesdorfer
;

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance Ignore ; Only allow one running instance
SetBatchLines -1 ; Don't break every 10ms to wait 10ms (balance the CPU). The script doesn't use much anyway.
; #MaxHotkeysPerInterval 20

/* 
	Changelog

	Beta 1 -> 2 (so far)
		Enlarged the text box height (20 to 40) in the new issue window. Adjusted button/window size accordingly. 
		Noticed the app wasn't actually writing the default comment data. Oops!

		
*/
; Reader note: Code flow stops at return statements

;reg query "hklm\system\controlset001\control\nls\language" /v Installlanguage
; http://www.science.co.il/language/locale-codes.asp


; List of GUI Numbers (to avoid conflicts)
; 1 - Listview
; 2 - Options
; 3 - New Issue
; 4 - Language Config
; 5+ - Viewed Issue(s) 
window_nums=5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99

; Declare global variables for listview coloring
; Loop,Parse,window_nums,`,
	; LV_%A_loopfield%=
; comment_LV_var=
; issue_listview=

; When user attempts to exit, go to the label "exitblock" (at the bottom of this file)
OnExit,exitblock


; Set program name here (for easy changing)
app_name := "Issue Tracker Beta 1 (5/8/2011)"

; Location of top data directory (no trailing slash, can be relative)
data_dir := ".\Data"

; Enables ÜñîçÔdË in written files (namely in AHK's FileAppend, which the INI lib uses)
FileEncoding,UTF-8

; Prompt to create data files/folders if they don't exist
IfNotExist, %data_dir%
{
	; Allow a dynamic GUI
	updatewelcomewindow:
	
	; If the window exists, read the language from there
	If (welcome)
	{
		ControlGet,language,Choice,,Combobox1,%welcome%
		oldname=%welcome%
	}
	Else
		language=English(Issues) ; Otherwise, load the default language file

	; Read the strings
	;Msgbox, %language%
	FileRead,language_strings,.\languages\%language%.ini

	welcome := get_language_string("welcome",language)
	introblock := get_language_string("introblock",language)
	lang_description := get_language_string("languagedescription",language)
	continue := get_language_string("continue",language)
	exit := get_language_string("exit",language)

	IfWinExist, %oldname% ; If the gui is already open...
	{
		; ...Update it!
		GuiControl,text,button1,%continue%
		GuiControl,text,button2,%exit%		
		GuiControl,text,static1,%introblock% ; ,%oldname%
		GuiControl,text,static2,%lang_description%
		WinSetTitle,%oldname%,,%welcome%
		Return
	}
	Else ; If not, make it!
	{
		; Build language list
		language_list =
		num_languages = 0
		Loop,.\languages\*.ini
		{
			; Remove extension
			StringTrimRight, filename, A_loopfilename, 4
			
			If (filename = language) ; Set user selection as default
				language_list=%language_list%%filename%||
			Else
				language_list=%language_list%%filename%|
			
			; Increment counter for DDL size
			num_languages+=1
		}

		; Display GUI
		Gui,-Theme
		
		; Welcome text
		Gui, Add, Text, x22 y10 w260 h30 +center, %introblock%
		
		; DDL/combobox language list
		Gui, Add, DropDownList, x72 y40 w160 h30 vuser_language r%num_languages% gupdatewelcomewindow, %language_list%
		
		; Descriptive text
		Gui,Add,Text, x22 y80 w260 h40,%lang_description%
		
		; Buttons
		Gui, Add, Button, x22 y130 w120 h40 gsetuserlanguage, %continue%
		Gui, Add, Button, x162 y130 w120 h40 gexitblock, %exit%
		; Generated using SmartGUI Creator 4.0
		Gui, Show, h195 w307, %welcome%
		Return
	}

	GuiClose:
	ExitApp
	
	setuserlanguage:
	Gui, submit
	; Create user pref file based on language choice - these are not indented here to avoid indenting in the file
	userprefs=
	(
#Filler line to avoid reading issues
[language]
current=%user_language%
	)
	
	; Create default (empty) issues file
	issues=
	(
	# App won't acknowledge the issues section if it's on line one`, (UTF-8 symbols?) so here's a filler line :)
[issues]
number=0
open=
closed=
temp=
	)
	
	; Create default (empty) comments file
	comments=
	(
# App won't acknowledge the comments section if it's on line one`, (UTF-8 symbols?) so here's a filler line :)
[comments]
number=0
temp=
	)
	
	; Create folder structure
	FileCreateDir, %data_dir%
	FileCreateDir, %data_dir%\Issues\
	
	; Write files
	FileAppend,%issues%,%data_dir%\Issues\issues.ini 
	FileAppend,%comments%,%data_dir%\Issues\comments.ini
	FileAppend,%userprefs%,%data_dir%\userprefs.ini
	
	; Restart program
	Reload 
	Return
}




; Add the all important libraries
#Include .\Libs\ini.ahk
;#Include .\Libs\LVA.ahk

; Needed for LVA
;OnMessage("0x4E", "LVA_OnNotify")

; Add the GUI functions and language support
#Include .\GUIs\listissues.ahk
#Include .\GUIs\languages.ahk
#Include .\GUIs\newissue.ahk
#Include .\GUIs\viewissue.ahk

; Read the configuration file(s)
FileRead,user_prefs,%data_dir%\userprefs.ini
language := get_active_language()

; Load the appropriate language file
FileRead,language_strings,.\languages\%language%.ini

; Set Window titles
; Issue list
issue_win_title := get_language_string("issuelist",language) . " - " app_name

; New issue
new_issue_win_title := get_language_string("newissue",language) . " - " app_name


; Load issues/comments
FileRead,issues,%data_dir%\Issues\issues.ini
FileRead,comments,%data_dir%\Issues\comments.ini

;Bring up the issue list
show_issue_list()
Return

; Debugging
;#i::Msgbox, %issues%
;#c::Msgbox, %comments%

; Causes F5 to refresh the issue list if that window is active
$F5::
IfWinActive, %issue_win_title%
	list_issues()
IfWinNotActive, %issue_win_title% ; Otherwise, handle f5 normally
	Send, {F5}
Return

;  Code to be run on exit
exitblock:

; Merge temp files with core issue data
issue_ini_path=%data_dir%\issues\issues.ini
IniRead,temp_list,%issue_ini_path%,issues,temp ; Read the temp list
Loop,parse,temp_list,`,
{
	If (A_Loopfield != "")
	{ 	; Read each issues data, merge it into the main data file, then delete the temp one
		FileRead,temp_issue,%data_dir%\Issues\issue%A_loopfield%.ini
		ini_mergekeys(issues,temp_issue)
		FileDelete,%data_dir%\Issues\issue%A_loopfield%.ini
	}
}

; Clean up temp list, then write issue INI
ini_replaceValue(issues,"issues","temp","")
ini_save(issues,issue_ini_path)


; Merge temp comments with primary data file
comment_ini_path=%data_dir%\issues\comments.ini
IniRead,temp_list,%comment_ini_path%,comments,temp ; Read the temp list
Loop,parse,temp_list,`,
{
	If (A_Loopfield != "")
	{ 	; Read each issues data, merge it into the main data file, then delete the temp one
		FileRead,temp_comment,%data_dir%\Issues\comment%A_loopfield%.ini
		ini_mergekeys(comments,temp_comment)
		FileDelete,%data_dir%\Issues\comment%A_loopfield%.ini
	}
}

; Clean up temp list, then write issue INI
ini_replaceValue(comments,"comments","temp","")
ini_save(comments,comment_ini_path)


; Forgetting this important line causes the app to never close...
; ...but it still gets forgotten sometimes ;)
ExitApp 
Return




; I don't need the prompt as I make it, but others may find it helpful
; Will make it an option later
; If A_IsCompiled
	; Msgbox,4,, Close Issue Tracker?
		; IfMsgbox Yes
		; {
			; Gui,Destroy
			;Write INI
			; issue_ini_path=%data_dir%\issues\issues.ini
			; ini_save(issues,issue_ini_path)
			; ExitApp
		; }
; Else
; {
	
	; ExitApp
; }
; Return